
// pages/home/home.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userinfo: getApp().userinfo,
        has_bind: false,
        showBindUserInfoBanner: false,
        user_no: '',
        user_name: '',
        showSelectExamSheet: false,
        myExamList: [],
        myExamSheetList: []
    },
    userNoInput(e) {

        this.setData({
            user_no: e.detail.value
        })
    },
    userNameInput(e) {

        this.setData({
            user_name: e.detail.value
        })
    },
    confirmBindUserInfo() {
        if (!this.data.user_no) {
            wx.su.toast("请输入正确的工号");
            return false;
        }
        if (!this.data.user_name) {
            wx.su.toast("请输入正确的姓名");
            return false;
        }
        let that = this;
        this.data.userinfo.user_no = this.data.user_no;
        this.data.userinfo.user_name = this.data.user_name;
        wx.scommon.rpost(wx.surls.user.bind, this.data.userinfo, function (res) {
            that.setData({
                userinfo: res.data,
                showBindUserInfoBanner: false
            })
            getApp().writeUserInfo(res.data);
        }, function (err) {
            console.log(err);
            if (err.statusCode == 602) {
                wx.su.toast("该工号已被绑定");
            } else if (err.statusCode == 407) {
                wx.su.toast("该工号不存在，请检查输入是否正确");
            } else {
                wx.su.toast("数据加载失败，请检查网络后重试");
            }
        }, true);
    },
    bindUserInfo() {
        let that = this;
        if (this.data.userinfo.has_bind == 1) {
            return false;
        }
        if (getApp().userinfo.nick_name) {
            that.setData({
                showBindUserInfoBanner: true
            });
            return false;
        }
        wx.scommon.getUserProfile(
            function () {
                that.setData({
                    userinfo: getApp().userinfo,
                    showBindUserInfoBanner: true
                })
            }
        );
    },
    closeBindUserInfoBanner() {
        this.setData({
            showBindUserInfoBanner: false
        })
    },
    createOrder() {
        let that = this;
        if (!getApp().userinfo || getApp().userinfo.has_bind != 1) {
            wx.showModal({
                title: "提示",
                confirmText: "去绑定",
                content: "您尚未绑定职工信息，请绑定后操作",
                showCancel: false,
                success: function () {
                    that.bindUserInfo();
                },
            })
            return false;
        }
        this.selectStore(function (stores) {
            if (stores && stores.length == 1) {
                wx.navigateTo({
                    url: "/pages/materials/materials?storeId=" + stores[0].id
                })
            } else {
                let itemList = [];
                for (let item of stores) {
                    itemList.push(item.name)
                }
                wx.showActionSheet({
                    itemList: itemList,
                    success: function (res) {
                        wx.navigateTo({
                            url: "/pages/materials/materials?storeId=" + stores[res.tapIndex].id
                        })
                    }
                })
            }

        })

    },
    selectStore(callback) {
        if (!getApp().globalData.stores || getApp().globalData.stores.length == 0) {
            wx.scommon.rget(wx.surls.store.getStoreByLoginUserId.add(this.data.userinfo.id), function (res) {
                getApp().globalData.stores = res.data;

                callback(getApp().globalData.stores);
            }, function (err) {})
        } else {
            callback(getApp().globalData.stores);
        }
    },
    openMyRecords() {
        let that = this;
        if (!getApp().userinfo || getApp().userinfo.has_bind != 1) {
            wx.showModal({
                title: "提示",
                confirmText: "去绑定",
                content: "您尚未绑定职工信息，请绑定后操作",
                showCancel: false,
                success: function () {
                    that.bindUserInfo();
                },
            })
            return false;
        }

        wx.navigateTo({
            url: "/pages/orderRecords/orderRecords"
        })
    },

    openApproveOrders() {
        let that = this;
        if (!getApp().userinfo || getApp().userinfo.has_bind != 1) {
            wx.showModal({
                title: "提示",
                confirmText: "去绑定",
                content: "您尚未绑定职工信息，请绑定后操作",
                showCancel: false,
                success: function () {
                    that.bindUserInfo();
                },
            })
            return false;
        }

        wx.navigateTo({
            url: "/pages/approveOrders/approveOrders"
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (!getApp().userinfo) {
            this.setData({
                has_bind: false
            })
        } else {
            this.setData({
                has_bind: true,
                userinfo: getApp().userinfo
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        
        wx.hideHomeButton()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})